// pages/settings/settings.js
const app = getApp()

Page({
  data: {
    userSettings: {
      notifications: true,
      orderNotifications: true,
      messageNotifications: true,
      soundEnabled: true,
      vibrationEnabled: true,
      autoAcceptOrders: false,
      showOnlineStatus: true,
      allowLocationAccess: true
    }
  },

  onLoad() {
    this.loadUserSettings()
  },

  loadUserSettings() {
    // 从本地存储加载设置
    const settings = wx.getStorageSync('userSettings')
    if (settings) {
      this.setData({
        userSettings: { ...this.data.userSettings, ...settings }
      })
    }
  },

  onNotificationChange(e) {
    const { field } = e.currentTarget.dataset
    const value = e.detail.value
    
    this.setData({
      [`userSettings.${field}`]: value
    })
    
    this.saveSettings()
  },

  saveSettings() {
    wx.setStorageSync('userSettings', this.data.userSettings)
    wx.showToast({
      title: '设置已保存',
      icon: 'success',
      duration: 1000
    })
  },

  clearCache() {
    wx.showModal({
      title: '清除缓存',
      content: '确定要清除所有缓存数据吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({ title: '清除中...' })
          
          // 模拟清除缓存
          setTimeout(() => {
            wx.hideLoading()
            wx.showToast({
              title: '缓存已清除',
              icon: 'success'
            })
          }, 1500)
        }
      }
    })
  },

  checkUpdate() {
    wx.showLoading({ title: '检查中...' })
    
    // 模拟检查更新
    setTimeout(() => {
      wx.hideLoading()
      wx.showModal({
        title: '检查更新',
        content: '当前已是最新版本',
        showCancel: false
      })
    }, 1000)
  },

  aboutApp() {
    wx.navigateTo({
      url: '/pages/about/about'
    })
  },

  contactService() {
    wx.showActionSheet({
      itemList: ['在线客服', '电话客服', '意见反馈'],
      success: (res) => {
        switch (res.tapIndex) {
          case 0:
            wx.navigateTo({
              url: '/pages/chat/chat?userId=service&userName=在线客服'
            })
            break
          case 1:
            wx.makePhoneCall({
              phoneNumber: '400-123-4567'
            })
            break
          case 2:
            wx.navigateTo({
              url: '/pages/feedback/feedback'
            })
            break
        }
      }
    })
  },

  privacyPolicy() {
    wx.navigateTo({
      url: '/pages/privacy/privacy'
    })
  },

  userAgreement() {
    wx.navigateTo({
      url: '/pages/agreement/agreement'
    })
  },

  async logout() {
    wx.showModal({
      title: '退出登录',
      content: '确定要退出当前账号吗？',
      success: async (res) => {
        if (res.confirm) {
          wx.showLoading({ title: '退出中...' })
          
          try {
            // 调用app中的登出方法
            const success = await app.utils.logout()
            
            wx.hideLoading()
            
            if (success) {
              wx.showToast({
                title: '已退出登录',
                icon: 'success',
                success: () => {
                  setTimeout(() => {
                    wx.reLaunch({
                      url: '/pages/login/login'
                    })
                  }, 1500)
                }
              })
            } else {
              wx.showToast({
                title: '退出失败，请重试',
                icon: 'none'
              })
            }
          } catch (error) {
            wx.hideLoading()
            console.error('登出失败:', error)
            wx.showToast({
              title: '退出失败，请重试',
              icon: 'none'
            })
          }
        }
      }
    })
  }
})